﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace 学生选课及成绩查询管理系统
{
    public partial class addCourseInfo : Form
    {
        public addCourseInfo()
        {
            InitializeComponent();
        }

        private void btn_sub_Click(object sender, EventArgs e)
        {
            string cname = txt_cname.Text;
            string tno = txt_tno.Text;
            string credit = txt_credit.Text;
            string cproperties = txt_cproperties.Text;
            if (string.IsNullOrEmpty(cname) || string.IsNullOrEmpty(tno) || string.IsNullOrEmpty(credit))
            {
                MessageBox.Show("请输入完整信息");
                return;
            }

            string sql = "insert into course(cname,tno,credit,cproperties) values(@cname,@tno,@credit,@cproperties)";
            SqlParameter[] sqlarray = { new SqlParameter("@cname",cname),new SqlParameter("@tno",tno),
                                          new SqlParameter("@credit",credit),new SqlParameter("@cproperties",cproperties)};

            int res = DBAccess.GetState(sql, sqlarray);
            if (res != 0)
            {
                MessageBox.Show("成功添加课程信息");
                addCourseInfo_Load(sender,e);
            }
            else
            {
                MessageBox.Show("添加失败");
            }

        }

        private void txt_credit_Click(object sender, EventArgs e)
        {
            string tno = txt_tno.Text;
            if (tno=="")
            {
                return;
            }
            string sql = "select count(*) from teacher where tno="+tno;
            object res=DBAccess.GetOne(sql);
            if((int)res==0){
                MessageBox.Show("没有该老师！请重新填写");
                txt_tno.Focus();
                return;
            }
        }

        private void txt_credit_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addCourseInfo_Load(object sender, EventArgs e)
        {
            string sql = "select * from course";
            DataTable dt = DBAccess.GetTable(sql);
            dataGridView1.DataSource = dt;
        }

        private void txt_cCount_TextChanged(object sender, EventArgs e)
        {

        }

        private void txt_cproperties_MouseLeave(object sender, EventArgs e)
        {
            while (true)
            {
                    if (txt_cproperties.Text == "选修")
                {
                    txt_cCount.Enabled = true;
                    break;
                }
                else
                {
                    txt_cCount.Enabled = false;
                    break;
                }
            }
        }

        private void txt_cCount_MouseHover(object sender, EventArgs e)
        {
            
        }

        private void txt_cCount_Click(object sender, EventArgs e)
        {
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = dataGridView1.CurrentCellAddress.Y + 1;
            int col = dataGridView1.CurrentCellAddress.X + 1;
            txt_cname.Text = dataGridView1.Rows[row - 1].Cells[1].Value.ToString();
            txt_tno.Text = dataGridView1.Rows[row - 1].Cells[2].Value.ToString();
            txt_credit.Text = dataGridView1.Rows[row - 1].Cells[3].Value.ToString();
            txt_cproperties.Text = dataGridView1.Rows[row - 1].Cells[4].Value.ToString();
            txt_cCount.Text = dataGridView1.Rows[row - 1].Cells[5].Value.ToString();
        }
    }
}
